package com.tencent.cloud.iov.util;

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * @author vinsonhuang
 * @date 2018/8/15
 */
public class FragmentUtils {

    /**
     * 使用show() hide()切换页面
     * 显示fragment
     */
    public static void showFragment(FragmentManager fragmentManager, Fragment hideFragment,
                                    Fragment showFragment, @IdRes int containerViewId,
                                    boolean isBack) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (isBack) {
            transaction.setCustomAnimations(com.qmuiteam.qmui.R.anim.slide_in_left,
                    com.qmuiteam.qmui.R.anim.slide_out_right);
        } else {
            transaction.setCustomAnimations(com.qmuiteam.qmui.R.anim.slide_in_right,
                    com.qmuiteam.qmui.R.anim.slide_out_left);
        }

        if (null != hideFragment) {
            transaction.hide(hideFragment);
        }

        if (showFragment.isAdded()) {
            transaction.show(showFragment);
        } else {
            transaction.add(containerViewId, showFragment);
        }
        transaction.commit();
    }
}
